// pages/cart/cart.js
let network = require("../../utils/network.js");
let app = getApp();
let url = app.globalData.url;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shoplist: [],// 购物车列表
    hasList: false,   // 列表是否有数据
    totalPrice: 0,   // 总价，初始为0
    selectAllStatus: false, // 全选状态，默认全选
    discount: 0,
    goods_id:0,
    change:2,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    that.setData({
      discount: wx.getStorageSync('discount')
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this
    that.cartlist()
  },

  bindlist:function(){
    wx.switchTab({
      url: '/pages/classify/classify',
    })
    return
  },

  cartlist(){
    var that = this
    wx.request({
      url: url + '/AppletsApi/Goods/shopCartList.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (data) {
        that.setData({
          shoplist: data.data.data
        })
        that.getTotalPrice()
      }
    })
  },

  // 计算总价
  getTotalPrice() {
    let carts = this.data.shoplist;     // 获取购物车列表
    let total = 0;
    for (let i = 0; i < carts.length; i++) {   // 循环列表得到每个数据
      if (carts[i].selected) {     // 判断选中才会计算价格
        total += carts[i].goods_num * carts[i].goods_price;  // 所有价格加起来
      }
    }
    this.setData({        // 最后赋值到data中渲染到页面
      shoplist: carts,
      totalPrice: total.toFixed(2)
    });
  },
  // 单选选择事件
  selectList(e) {
    var that = this
   var aa = []
    const index = e.currentTarget.dataset.index; // 获取data- 传进来的index
    const id = e.currentTarget.dataset.id
    // let carts = this.data.shoplist;     // 获取购物车列表
    // const selected = carts[index].selected;   // 获取当前商品的选中状态
    // carts[index].selected = !selected;    // 改变状态

    // if (carts[index].selected == true){
    //   aa.push(carts[index].id)
    // }
    // console.log(aa)
    
    const select = e.currentTarget.dataset.select
    let change
    if(select){
      change = 2
    }else{
      change = 1    
    }
    console.log(select, change)
    // this.setData({
    //   shoplist: carts,
    // });
    this.getTotalPrice();  
    that.setData({
      change: change
    })
      network.request({
        url: url +'/AppletsApi/Goods/changeShopCart.html',
        data: {
          applet_openid: wx.getStorageSync('applet_openid'),
          shopcart_id: id,
          change: change,
        },
        method: 'post',
        header: {
          "content-type": "application/x-www-form-urlencoded"
        },
        success: function (data) {
          that.cartlist()
        }
      })

     // 重新获取总价
  },

  // 增加数量
  addCount(e) {
    let id = e.currentTarget.dataset.id;
    let index = e.currentTarget.dataset.index;
    let carts = this.data.shoplist;
    let num = carts[index].goods_num;
    num = Number(num) + Number(1)
   carts[index].goods_num = num;
    
    this.setData({
      shoplist: carts
    });

    wx.request({
      url: url + '/AppletsApi/Goods/ShopCartNumEdit.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
        shopcart_id: id,
        goods_num: carts[index].goods_num
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (data) {

      }
    })

    this.getTotalPrice();
  },
  // 减少数量
  minusCount(e) {
    const index = e.currentTarget.dataset.index;
    let carts = this.data.shoplist;
    let num = carts[index].goods_num;
    if (num <= 1) {
      return false;
    }
    num = Number(num) - Number(1)
    carts[index].goods_num = num;
    this.setData({
      shoplist: carts
    });
    
    const id = e.currentTarget.dataset.id;
    wx.request({
      url: url + '/AppletsApi/Goods/ShopCartNumEdit.html',
      data: {
        applet_openid: wx.getStorageSync('applet_openid'),
        shopcart_id:id,
        goods_num: carts[index].goods_num
      },
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      success: function (data) {

      }
    })
    this.getTotalPrice();
  },

  deleteList(e) {
    var that = this
    const index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '提示',
      content: '确认删除吗',
      success(res) {
        if (res.confirm) {
          // 删除
          wx.request({
            url: url + '/AppletsApi/Goods/delToCart.html',
            data: {
              applet_openid: wx.getStorageSync('applet_openid'),
              shopcart_id: index,
            },
            method: 'post',
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            success: function (data) {
              wx.request({
                url: url + '/AppletsApi/Goods/shopCartList.html',
                data: {
                  applet_openid: wx.getStorageSync('applet_openid'),
                },
                method: 'post',
                header: {
                  "content-type": "application/x-www-form-urlencoded"
                },
                success: function (data) {
                  for (var i = 0; i < data.data.data.length; i++) {
                    data.data.data[i].selected = true
                  }
                  that.setData({
                    shoplist: data.data.data
                  })
                }
              })
            }
          })
        } else if (res.cancel) {
          
        }
      }
    })

  },
  collectsite:function(e){
    var that = this
    const path = e.currentTarget.dataset.path;
    if (that.data.totalPrice == 0){
      wx.showToast({
        title: '您还没有选择商品哦',
        icon:'none'
      })
    }else{
      wx.navigateTo({
        url: path,
      })
        }
      
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  detail:function(e){
    let id = e.currentTarget.dataset.index
    wx.navigateTo({
      url: id,
    })
  }


})